/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer II 003. 前 n 个数字二进制中 1 的个数
 * @author: WXY
 * @create: 2022-12-18 20:41
 * @Version 1.0
 **/

public class offer_one3_countBits {
    public static int[] countBits(int n) {
        int[] ans = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            ans[i] = oneNumBit(i);
        }
        return ans;
    }

    private static int oneNumBit(int num) {
        int res = 0;
        for (int i = 0; i < 32; i++) {
            if ((num & (1 << i)) != 0) {
                res++;
            }
        }
        return res;
    }

    public static int[] countBits1(int n) {
        int[] dp = new int[n + 1];
        dp[0] = 0;
        if (n == 0 ) return dp;
        dp[1] = 1;
        for (int i = 2; i <= n; i++) {
            if (i % 2 == 0) {
                dp[i] = dp[i >> 1];
            } else {
                dp[i] = dp[i - 1] + 1;
            }
        }
        return dp;
    }


    public static void main(String[] args) {
        int[] ints = countBits(2);
        for (int i : ints) {
            System.out.println(i);

        }
    }
}
